
<script>
/**
 * 表格组件
 */
export default {
  name: 'ym-table',
  props: {
    /**
     * 数据源
    **/
    data: {
      type: Array,
      default: []
    },
    /**
     * 配置项，表格数据展示
     * 结构：
     * {
     *     config:[{
     *      prop:'',//key值，
     *      label:'',//标题
     *      selectable：Function  // 禁用列
     *      width:'',//跟elementui值一样
     *      align:'',//跟elementui值一样
     *      reserveSelection:true //记录页面跳转前所选择的数据
     *      render:(h,params)=>{}, //按钮，文本转换等，格式跟react类似
     *     }]
     * }
     */
    options: {
      type: Object,
      default: {
        config: [],
      }
    },
    /**
     * 当前页
     */
    currentPage: {
      type: Number,
      default: 1
    },
    /**
     * 当前页展示数量
     */
    perPage: {
      type: Number,
      default: 12
    },
    /**
     * 总数量
     */
    total: {
      type: Number,
      default: 0
    },
    /**
     * false不显示分页组件
     */
    hasPagination: {
      type: Boolean,
      default: true
    },
    /**
     * 加载状态
     */
    loading: {
      type: Boolean,
      default: false
    },
    /**
     * 列表页接口函数
     */
    load: {
      type: Function,
      default: () => { }
    },
    /**
     * 分页数量选择
     */
    pageSizes: {
      type: Array,
      default: () => {
        return [2, 12, 24, 36, 48, 100]
      }
    },
    /**
     * 表格样式
     */
    border: {
      type: Boolean,
      default: () => {
        return false
      }
    },
    /**
     * 默认排序
     */
    defaultSort: {
      type: Object,
      default: () => {
        return {}
      }
    },
    /**
     * 表格最大高度
     */
    maxHeight: {
      type: [String, Number],
      default: window.innerHeight - 315,
    },
    /**
    * maxHeight基础上再减去的高度
    */
    reduceHeight: {
      type: [String, Number],
      default: 0
    },
    highlightCurrentRow: {
      type: Boolean,
      default: true
    },
    size: {
      default: ''
    },
    setRouterPage: {
      type: Boolean,
      default: true
    },
    /**
     * 是否设置页数量
     */
    setRouterSize: {
      type: Boolean,
      default: true
    },
    //限制高度
    limitHeight: {
      type: Boolean,
      default: false
    },
    height: {
      type: [String, Number],
      default: '',
    },
    headerCellStyle: {
      default: () => {
        return { background: "#F5F7FA" }
      }
    }
  },
  data () {
    return {
      perPageValue: this.perPage,
      currentPageValue: this.currentPage,
    }
  },
  watch: {
    currentPage (v) {
      this.currentPageValue = v
      if (this.setRouterPage) {
        this.$router.replace({
          query: {
            ...this.$route.query,
            page: v,
          },
        })
      }
    },
    perPage (v) {
      this.perPageValue = v
      if (this.setRouterSize) {
        this.$router.replace({
          query: {
            ...this.$route.query,
            size: v,
          },
        })
      }
    }
  },
  // created () {
  //   if (this.$route.query.page) {
  //     this.currentPageValue = Number(this.$route.query.page)
  //   }
  // },
  render (h) {
    return <div class="m-block-view--table-cont1">
      <el-table data={this.data} v-loading={this.loading}
        ref="multipleTable"
        style="width: 100%"
        stripe
        border={this.border}
        default-sort={this.defaultSort}
        on-selection-change={this.toggleAllSelection}
        row-key={this.getRowKeys}
        on-sort-change={this.sortChange}
        max-height={this.limitHeight ? this.maxHeight - Number(this.reduceHeight) : '100%'}
        height={this.height || null}
        size={this.size}
        highlight-current-row={this.highlightCurrentRow}
        on-current-change={this.handleCurrentRowChange}
        header-cell-style={this.headerCellStyle}
        on-cell-dblclick={this.cellDblclick}
      >
        {this.$slots.empty && <div slot="empty" style={`padding:10px 0`}>{this.$slots.empty}</div>}
        {
          this.options.config.map((item, index) => {
            return <el-table-column
              align={item.align}
              key={index}
              prop={item.prop}
              min-width={item.minWidth}
              width={item.width}
              label={item.label}
              fixed={item.fixed}
              sortable={item.sortable || false}
              type={item.type || ''}
              selectable={item.selectable}
              render-header={item.renderHeader && item.renderHeader}
              reserve-selection={item.reserveSelection || true}
            >
              {
                item.list ? item.list.map((v, vIndex) => {
                  return <el-table-column
                    align={v.align}
                    key={vIndex}
                    prop={v.prop}
                    min-width={v.minWidth}
                    width={v.width}
                    label={v.label}
                    fixed={v.fixed}
                    sortable={v.sortable || false}
                    type={v.type || ''}
                    selectable={v.selectable}
                    render-header={v.renderHeader && v.renderHeader}
                    reserve-selection={v.reserveSelection || true}
                  >
                    {
                      v.popover ? scope => {
                        return <el-popover trigger={v.popoverProp && v.popoverProp.trigger ? v.popoverProp.trigger : 'hover'} width={v.popoverProp ? v.popoverProp.width : '500'}>
                          {v.render ? vIndex.render(h, scope, this) : scope.row[v.prop]}
                          <div slot='reference'><div class="ep1">{
                            v.render ? v.render(h, scope, this) : scope.row[v.prop]
                          }</div></div>
                        </el-popover>
                      } : v.render ? scope => v.render(h, scope, v, this) : ''
                    }
                  </el-table-column>
                }) : item.popover ? scope => {
                  return <el-popover trigger={item.popoverProp && item.popoverProp.trigger ? item.popoverProp.trigger : 'hover'} width={item.popoverProp ? item.popoverProp.width : '500'}>
                    {item.render ? item.render(h, scope, this) : scope.row[item.prop]}
                    <div slot='reference'><div class="ep1">{
                      item.render ? item.render(h, scope, this) : scope.row[item.prop]
                    }</div></div>
                  </el-popover>
                } : item.render ? scope => item.render(h, scope, item, this) : ''
              }
            </el-table-column>
          })
        }
      </el-table>
      {
        this.hasPagination && <div class="m-block-view--table-cont__pagination">
          <el-pagination
            background
            current-page={this.currentPageValue || 1}
            page-size={this.perPageValue}
            page-sizes={this.pageSizes}
            layout="sizes,total, prev, pager, next"
            total={this.total || 0}
            on-current-change={this.handleCurrentChange}
            on-size-change={this.handleSizeChange}
          />
        </div>
      }
    </div >
  },
  methods: {
    handleCurrentChange (val) {
      this.$emit('load', val, this.perPageValue)
    },
    handleSizeChange (val) {
      console.log(111)
      this.perPageValue = val
      this.$emit('load', 1, val)
    },
    /**
     * 列表多选
     */
    toggleAllSelection (e) {
      this.$emit('select', e)
      this.$emit('select-all', e)
      this.$emit('selection-change', e)
    },
    getRowKeys (row) {
      return row.rowKeys || row.id
    },
    /**
     * 清空
     */
    clearSelection () {
      this.$refs.multipleTable.clearSelection();
    },
    sortChange (val) {
      this.$emit('sortChange', val)
    },
    /**
     * 选中当前行
     */
    handleCurrentRowChange (e) {
      this.$emit('current-change', e)
    },
    /**
     * 双击单元格
     */
    cellDblclick (row, column, cell, event) {
      if (!['操作'].includes(column.label)) {
        this.$emit('cell-dblclick', row, column, cell, event)
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.m-block-view--table-cont__pagination {
  text-align: right;
  padding: 8px 0;
}
th.el-table__cell {
  padding: 8px 0 !important;
  color: #000;
}
.el-table__fixed-body-wrapper {
  z-index: auto !important;
}
</style>
